﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using IEx.Common;
using IEx.Model.Partials;
using IEx.Utilities;

namespace IEx.ParserBuilder.Raovat
{
    public class RaovatParser : IHtmlParser
    {
        public void Parse(SourceItem item, string html, Action<Announcement> attachResultItem, Action<SourceItem> attachNewItemSource)
        {
            try
            {
                string startHtml = "id=\"VipAds\"";
                string endHtml = "id=\"pCounter\"";

                string remaintHtml = Utility.GetHtmlBody(html, startHtml, endHtml);
                List<Announcement> detectedAnnouncements = new List<Announcement>();
                Uri newUri = Utility.CreateNewUriForNextPage(item.Url);

                if (!string.IsNullOrEmpty(remaintHtml))
                {
                    Regex regAds = new Regex("<a class=\"adtype.*?\" href=\".*?\" title=\".*?\">.*?</a>");
                    MatchCollection mAds = regAds.Matches(remaintHtml);
                    for (int i = 0; i < mAds.Count; i++)
                    {
                        string titleAds = string.Empty;
                        string linkAds = string.Empty;
                        string contentAds = string.Empty;

                        string adsHtml = mAds[i].Value.Trim();
                        titleAds = RegexUtility.ExtractTextFromHtmlTag(adsHtml);
                        linkAds = new Uri(newUri, RegexUtility.ExtractHyperlink(adsHtml)).ToString();
                        if (!string.IsNullOrEmpty(linkAds))
                        {
                            contentAds = ExtractAdsDetails(linkAds, item);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // if has an error, save fail request to next time scanning
                if (item.FailId == 0)
                {
                    item.FailId = DateTime.Now.Ticks;
                    Utility.SaveFailRequests(item);
                }

                // then write log
                string message = string.Format(@"
            Company code:   {0}
            HTML Parsing:   Fail
            Request URL:    {1}
            Search key:     {2}", item.SourceCode, item.Url, item.SearchCode);
                Logger.WriteError(message, ex);
            }
        }

        private string ExtractAdsDetails(string url, SourceItem item)
        {

            Logger.Debug("***SCANNING NEWS CLASSIFIEDADS DETAILS URL: " + url);
            HttpStatusCode status = HttpStatusCode.Accepted;
            string html = Utility.LoadHtml(url, ref status);
            string contentAds = string.Empty;
            try
            {
                string startHtml = "id=\"VipAds\"";
                string endHtml = "id=\"pCounter\"";
                string remaintHtml = Utility.GetHtmlBody(html, startHtml, endHtml);

                if (!string.IsNullOrEmpty(remaintHtml))
                {
                    Regex regAd = new Regex("<div id=\"site-descr\">(.|\n)*?<g");
                    Match mAd = regAd.Match(remaintHtml);
                    if (mAd.Success)
                    {
                        string adsHtml = mAd.Value.Trim();
                        contentAds = RegexUtility.ExtractTextFromHtmlTag(adsHtml);
                    }

                    Regex regVals = new Regex("<div class=\"value\">.*?</div>");
                    MatchCollection mVals = regVals.Matches(remaintHtml);
                    for (int j = 0; j < mVals.Count; j++)
                    {
                        string valsHtml = mVals[j].Value.Trim();
                        
                        Regex regPlace = new Regex("");
                        Match mPlace = regPlace.Match(valsHtml);
                        if (mPlace.Success)
                        {

                        }
                        if (j == 5)
                        {
                        }
                    }
                }
                return contentAds;
            }
            catch (Exception ex)
            {
                // if has an error, save fail request to next time scanning
                if (item.FailId == 0)
                {
                    item.FailId = DateTime.Now.Ticks;
                    Utility.SaveFailRequests(item);
                }

                // then write log
                string message = string.Format(@"
            Company code:   {0}
            HTML Parsing:   Fail
            Request URL:    {1}
            Search key:     {2}", item.SourceCode, item.Url, item.SearchCode);
                Logger.WriteError(message, ex);
                return string.Empty;
            }
        }

    }
}
